from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from news import models,newser
# Create your views here.
#所有分类
class Cate(APIView):
    def get(self,request):
        cate=models.Cate.objects.all()
        ser=newser.CateSerializer(cate,many=True)
        return Response({"msg":"ok","data":ser.data})
#更具分类查新闻
class News(APIView):
    def get(self,request):
        cate=request.query_params.get("cate")
        news=models.News.objects.filter(cate=cate)
        ser=newser.NewsSerializer(news,many=True)
        return Response({"msg":"ok","data":ser.data})
#拿到新闻详情
class NewsDetail(APIView):
    def get(self,request):
        id=request.query_params.get("id")
        news=models.News.objects.filter(id=id).first()#一个模型对象
        news.read+=1
        news.save()
        #news=models.News.objects.get(id=id)
        ser=newser.NewsSerializer(news)
        return Response({"msg":"ok","data":ser.data})